﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class FilterConfigDetailBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IFilterConfigDetailDA FilterConfigDA = DataAccess.CreateFilterConfigDetailDA();
        private static readonly IFilterConfigHeaderDA FilterConfigHeaderDA = DataAccess.CreateFilterConfigHeaderDA();
        private PubParasInfo pubParasInfo;

        public FilterConfigDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public FilterConfigDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public FilterConfigDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                FilterConfigDA.DeleteFilterOrderBy(dataBase, tran, mainID, subId, this.dataBaseID[0]);
                FilterConfigDA.DeleteFilterStatement(dataBase, tran, mainID, subId, this.dataBaseID[0]);
                FilterConfigDA.DeleteFilterConfigDetail(dataBase, tran, mainID, subId, this.dataBaseID[0]);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return subId[0];
        }

        public DataSet GetAllDetailInfo()
        {
            return FilterConfigDA.GetAllFilterConfigDetail(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object detailQuery)
        {
            FilterConfigDetailQueryEntity filterConfigDetail = detailQuery as FilterConfigDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(filterConfigDetail.FilterName))
            {
                sqlWhere.Add(" FILTER_NAME " + filterConfigDetail.FilterName);
            }
            if (!string.IsNullOrEmpty(filterConfigDetail.Descr))
            {
                sqlWhere.Add(" DESCR " + filterConfigDetail.Descr);
            }
            if (!string.IsNullOrEmpty(filterConfigDetail.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + filterConfigDetail.IsActive);
            }
            if (!string.IsNullOrEmpty(filterConfigDetail.SystemCreated))
            {
                sqlWhere.Add(" SYSTEM_CREATED " + filterConfigDetail.SystemCreated);
            }
            if (!string.IsNullOrEmpty(filterConfigDetail.RecordType))
            {
                sqlWhere.Add(" RECORD_TYPE " + filterConfigDetail.RecordType);
            }
            return FilterConfigDA.GetFilterConfigDetailByQueryList(sqlWhere, filterConfigDetail, this.dataBaseID[0]);
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            FilterConfigHeaderInfo FilterConfigHeader = new FilterConfigHeaderInfo();
            FilterConfigHeader = FilterConfigHeaderDA.GetFilterConfigHeaderByID(mainID, this.dataBaseID[0]);
            if (!string.IsNullOrEmpty(subID))
            {
                FilterConfigHeader.FilterConfigDetailInfos = FilterConfigDA.GetFilterConfigDetailByID(mainID, subID, this.dataBaseID[0]);
                FilterConfigHeader.FilterStatementInfos = FilterConfigDA.GetFilterStatementByID(mainID, subID, this.dataBaseID[0]);
                FilterConfigHeader.FilterOrderByInfos = FilterConfigDA.GetFilterOrderByByID(mainID, subID, this.dataBaseID[0]);
            }
            return FilterConfigHeader;
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            throw new NotImplementedException();
        }

        public string InsertDetail(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string StrFilterName = string.Empty;
            string StrRecordType = string.Empty;
            int StatementLine = 0;
            int OrderByLine = 0;
            try
            {
                FilterConfigHeaderInfo filterConfigHeaderInfo = obj as FilterConfigHeaderInfo;
                if (FilterConfigDA.GetFilterConfigDetailByID(dataBase, tran, filterConfigHeaderInfo.FilterConfigDetailInfos.RecordType, filterConfigHeaderInfo.FilterConfigDetailInfos.FilterName, this.dataBaseID[0]) != null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2175_001", this.pubParasInfo.UserLanguage, new List<string> { filterConfigHeaderInfo.FilterConfigDetailInfos.RecordType, filterConfigHeaderInfo.FilterConfigDetailInfos.FilterName });
                }
                StrRecordType = filterConfigHeaderInfo.FilterConfigDetailInfos.RecordType;
                StrFilterName = filterConfigHeaderInfo.FilterConfigDetailInfos.FilterName;
                filterConfigHeaderInfo.FilterConfigDetailInfos.SystemCreated = "N";
                filterConfigHeaderInfo.FilterConfigDetailInfos.IsActive = "Y";
                filterConfigHeaderInfo.FilterConfigDetailInfos.CreatedBy = filterConfigHeaderInfo.CreatedBy;
                filterConfigHeaderInfo.FilterConfigDetailInfos.CreatedDate = DateTime.Now;
                filterConfigHeaderInfo.FilterConfigDetailInfos.UpdatedBy = filterConfigHeaderInfo.FilterConfigDetailInfos.CreatedBy;
                filterConfigHeaderInfo.FilterConfigDetailInfos.UpdatedDate = DateTime.Now;
                FilterConfigDA.InsertFilterConfigDetail(dataBase, tran, filterConfigHeaderInfo.FilterConfigDetailInfos, this.dataBaseID[0]);
                if (filterConfigHeaderInfo.FilterStatementInfos != null)
                {
                    foreach (FilterStatementInfo StatementInfo in filterConfigHeaderInfo.FilterStatementInfos)
                    {
                        StatementLine += 10;
                        StatementInfo.RecordType = StrRecordType;
                        StatementInfo.FilterName = StrFilterName;
                        StatementInfo.LineID = StatementLine;
                        StatementInfo.CreatedBy = filterConfigHeaderInfo.CreatedBy;
                        StatementInfo.UpdatedBy = StatementInfo.CreatedBy;
                        FilterConfigDA.InsertFilterStatement(dataBase, tran, StatementInfo, this.dataBaseID[0]);
                    }
                }
                if (filterConfigHeaderInfo.FilterOrderByInfos != null)
                {
                    foreach (FilterOrderByInfo OrderByInfo in filterConfigHeaderInfo.FilterOrderByInfos)
                    {
                        OrderByInfo.RecordType = StrRecordType;
                        OrderByInfo.FilterName = StrFilterName;
                        OrderByLine += 10;
                        OrderByInfo.LineID = OrderByLine;
                        OrderByInfo.CreatedBy = filterConfigHeaderInfo.CreatedBy;
                        OrderByInfo.UpdatedBy = OrderByInfo.CreatedBy;
                        FilterConfigDA.InsertFilterOrderBy(dataBase, tran, OrderByInfo, this.dataBaseID[0]);
                    }
                }
                new AuditDataBLL().InsertAuditData<FilterConfigDetailInfo>(dataBase, tran, null, filterConfigHeaderInfo.FilterConfigDetailInfos, AuditAction.Insert, this.pubParasInfo.UserID, "FILTER_CONFIG_DETAIL", this.dataBaseID[0]);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return StrFilterName;
        }

        public string UpdateDetail(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string StrFilterName = string.Empty;
            string StrRecordType = string.Empty;
            int StatementLine = 0;
            int OrderByLine = 0;
            try
            {
                FilterConfigHeaderInfo filterConfigHeaderInfo = obj as FilterConfigHeaderInfo;
                FilterConfigDetailInfo oldFilterConfigDetailInfo = FilterConfigDA.GetFilterConfigDetailByID(dataBase, tran, filterConfigHeaderInfo.FilterConfigDetailInfos.RecordType, filterConfigHeaderInfo.FilterConfigDetailInfos.FilterName, this.dataBaseID[0]);
                List<string> RecordType = new List<string>();
                List<string> FilterName = new List<string> {
                    filterConfigHeaderInfo.FilterConfigDetailInfos.RecordType,
                    filterConfigHeaderInfo.FilterConfigDetailInfos.FilterName
                };
                FilterConfigDA.DeleteFilterOrderBy(dataBase, tran, RecordType, FilterName, this.dataBaseID[0]);
                FilterConfigDA.DeleteFilterStatement(dataBase, tran, RecordType, FilterName, this.dataBaseID[0]);
                FilterConfigDA.DeleteFilterConfigDetail(dataBase, tran, RecordType, FilterName, this.dataBaseID[0]);
                StrRecordType = filterConfigHeaderInfo.FilterConfigDetailInfos.RecordType;
                StrFilterName = filterConfigHeaderInfo.FilterConfigDetailInfos.FilterName;
                filterConfigHeaderInfo.FilterConfigDetailInfos.SystemCreated = "N";
                filterConfigHeaderInfo.FilterConfigDetailInfos.IsActive = "Y";
                filterConfigHeaderInfo.FilterConfigDetailInfos.CreatedBy = filterConfigHeaderInfo.CreatedBy;
                filterConfigHeaderInfo.FilterConfigDetailInfos.CreatedDate = DateTime.Now;
                filterConfigHeaderInfo.FilterConfigDetailInfos.UpdatedBy = filterConfigHeaderInfo.FilterConfigDetailInfos.CreatedBy;
                filterConfigHeaderInfo.FilterConfigDetailInfos.UpdatedDate = DateTime.Now;
                FilterConfigDA.InsertFilterConfigDetail(dataBase, tran, filterConfigHeaderInfo.FilterConfigDetailInfos, this.dataBaseID[0]);
                foreach (FilterStatementInfo StatementInfo in filterConfigHeaderInfo.FilterStatementInfos)
                {
                    StatementLine += 10;
                    StatementInfo.RecordType = StrRecordType;
                    StatementInfo.FilterName = StrFilterName;
                    StatementInfo.LineID = StatementLine;
                    StatementInfo.CreatedBy = filterConfigHeaderInfo.CreatedBy;
                    StatementInfo.UpdatedBy = StatementInfo.CreatedBy;
                    FilterConfigDA.InsertFilterStatement(dataBase, tran, StatementInfo, this.dataBaseID[0]);
                }
                foreach (FilterOrderByInfo OrderByInfo in filterConfigHeaderInfo.FilterOrderByInfos)
                {
                    OrderByInfo.RecordType = StrRecordType;
                    OrderByInfo.FilterName = StrFilterName;
                    OrderByLine += 10;
                    OrderByInfo.LineID = OrderByLine;
                    OrderByInfo.CreatedBy = filterConfigHeaderInfo.CreatedBy;
                    OrderByInfo.UpdatedBy = OrderByInfo.CreatedBy;
                    FilterConfigDA.InsertFilterOrderBy(dataBase, tran, OrderByInfo, this.dataBaseID[0]);
                }
                new AuditDataBLL().InsertAuditData<FilterConfigDetailInfo>(dataBase, tran, oldFilterConfigDetailInfo, filterConfigHeaderInfo.FilterConfigDetailInfos, AuditAction.Update, this.pubParasInfo.UserID, "FILTER_CONFIG_DETAIL", this.dataBaseID[0]);
                tran.Commit();
            }
            catch (Exception ex)
            {
                StrFilterName = "";
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return StrFilterName;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

